var chargingRecordStatisticsData = [];
var totalPage = 0;
var totalCnt = 0;

//查询
$(document).ready(function() {
	showLoading();
	$('title', parent.document).html($('title').text());
	setBaseUrl();
	createTableHead("chargingRecordStatisticsDetailThead", chargingRecordStatisticsDetailTable, search);
	search();
});
var lastheaderNameEN = undefined;
function search(headerNameEN){
	$('#equipmentNo').val($("#detailID", parent.document).val());
	$('#startChargingTime').val($("#startTime", parent.document).val());
	$('#endChargingTime').val($("#endTime", parent.document).val());
	if(lastheaderNameEN != headerNameEN){
		clearOrder(lastheaderNameEN);
		lastheaderNameEN = headerNameEN;
	}
	$ajax('searchChargingRecordStatisticsDetail', 'chargingRecordStatisticsDetailSearch', searchChargingRecordStatisticsDetail);
	$("#detailID", parent.document).val("");
	$("#startTime", parent.document).val("");
	$("#endTime", parent.document).val("");
}
function searchChargingRecordStatisticsDetail(data) {
	totalPage = data && data.totalPage || 0;
	totalCnt = data && data.totalCnt || 0;
	chargingRecordStatisticsData = data && data.chargingRecordStatisticsList || [];
	createTableBody("chargingRecordStatisticsDetailTbody", chargingRecordStatisticsDetailTable, chargingRecordStatisticsData);
	$("#chargingStationName").html("——" + chargingRecordStatisticsData[0].chargingStationName);
	$("#equipmentName").html("——" + chargingRecordStatisticsData[0].equipmentName);
	for (var i = 0; i < chargingRecordStatisticsData.length; i++){
		$("#balanceAmmeterNo" + i).html($("#endAmmeterNo" + i).html() - $("#startAmmeterNo" + i).html());
		$("#powerBalance" + i).html($("#balanceAmmeterNo" + i).html() - $("#totalChargingKWH" + i).html());
		if(chargingRecordStatisticsData[i].abnormalFlag == "1"){
//			$("#startChargingDTTM" + i).parent().parent()[0].bgColor = "#f19999";
			$("#startChargingDTTM" + i).css("background-color","#f19999");
			$("#startAmmeterNo" + i).css("background-color","#f19999");
			$("#endAmmeterNo" + i).css("background-color","#f19999");
			$("#balanceAmmeterNo" + i).css("background-color","#f19999");
			$("#totalChargingKWH" + i).css("background-color","#f19999");
			$("#powerBalance" + i).css("background-color","#f19999");
			$("#meterJump" + i).css("background-color","#f19999");
			$("#endChargingDTTM" + i).css("background-color","#f19999");
			$("#recenFlag" + i).css("background-color","#f19999");
			$("#markDTTM" + i).css("background-color","#f19999");
			$("#createDTTM" + i).css("background-color","#f19999");
		}
		if($("#powerBalance" + i).html() >= -10 && $("#powerBalance" + i).html() <= 10 && $("#powerBalance" + i).html() != 0){
			$("#powerBalance" + i).css("background-color","#f19999");
			$("#powerBalance" + i).css("color","#9999ff");
		} else if($("#powerBalance" + i).html() >10 || $("#powerBalance" + i).html() < -10){
			$("#powerBalance" + i).css("background-color","red");
			$("#powerBalance" + i).css("color","#9999ff");
		}
		if($("#startAmmeterNo" + i).html() == 0){
			$("#startAmmeterNo" + i).css("background-color","red");
			$("#startAmmeterNo" + i).css("color","#9999ff");
		} else if($("#endAmmeterNo" + i).html() == 0){
			$("#endAmmeterNo" + i).css("background-color","red");
			$("#endAmmeterNo" + i).css("color","#9999ff");
		}
		if($("#startAmmeterNo" + (i+1)).html()){
			if($("#endAmmeterNo" + i).html() != $("#startAmmeterNo" + (i+1)).html()){
				$("#endAmmeterNo" + i).css("background-color","yellow");
				$("#startAmmeterNo" + (i+1)).css("background-color","yellow");
				$("#endAmmeterNo" + i).css("color","#9999ff");
				$("#startAmmeterNo" + (i+1)).css("color","#9999ff");
			}
			$("#meterJump" + (i+1)).html($("#startAmmeterNo" + (i+1)).html() - $("#endAmmeterNo" + i).html());
			if($("#meterJump" + (i+1)).html() <= 6 && $("#meterJump" + (i+1)).html() != 0){
				$("#meterJump" + (i+1)).css("background-color","#e8e87b");
				$("#meterJump" + (i+1)).css("color","#9999ff");
			} else if($("#meterJump" + (i+1)).html() > 6){
				$("#meterJump" + (i+1)).css("background-color","yellow");
				$("#meterJump" + (i+1)).css("color","#9999ff");
			}
		}
	}
	var totalBalanceAmmeterNo = 0;
	var totalTotalChargingKWH = 0;
	var totalPowerBalance = 0;
	var totalMeterJump = 0;
    $('#chargingRecordStatisticsDetailTbody tr').each(function() {
        $(this).find('td:eq(4)').each(function(){
        	totalBalanceAmmeterNo += parseFloat($(this).text());
        });
        $(this).find('td:eq(5)').each(function(){
        	totalTotalChargingKWH += parseFloat($(this).text());
        });
        $(this).find('td:eq(6)').each(function(){
        	totalPowerBalance += parseFloat($(this).text());
        });
        $(this).find('td:eq(7)').each(function(){
        	if($(this).text() == "-"){
        		$(this).text(0);
        	}
        	totalMeterJump += parseFloat($(this).text());
        });
    });
    if(document.getElementById('chargingRecordStatisticsDetailThead').rows.length>1){
    	document.getElementById('chargingRecordStatisticsDetailThead').deleteRow(1);
    }
    $('#chargingRecordStatisticsDetailThead').append('<tr><th>合计</th><th></th><th></th><th></th><th>' + totalBalanceAmmeterNo + '</th><th>' + totalTotalChargingKWH + '</th><th>' + totalPowerBalance + '</th><th>' + totalMeterJump + '</th><th></th><th></th><th></th><th></th><th></th></tr>');  
	setTimeout(hideLoading, 500);
}
function showAbnormal(){
	var tbodtID = $('#chargingRecordStatisticsDetailTbody');
    var rowsLength = tbodtID[0].childElementCount;
	if(document.getElementById("showAbnormal").checked){
	    for(var i=0;i<rowsLength;i++){
	    	var showDisplay = false;
	    	for(var j=2;j<7;j++){
	    		var searchText = tbodtID[0].rows[i].children[j].children["0"].style.color;
	        	if(searchText != "" || chargingRecordStatisticsData[i].abnormalFlag == "1"){
	        		showDisplay = true;
	        		j = 7;
	        	}
	    	}
	    	if(!showDisplay){
	    		tbodtID[0].rows[i].style.display='none';
	    	}
	    }
	} else{
		for(var i=0;i<rowsLength;i++){
    		tbodtID[0].rows[i].style.display='';
	    }
	}
}
function seeMessage(index){
	if (chargingRecordStatisticsData[index].message != null){
		$("#message").val(chargingRecordStatisticsData[index].message.replace(/,/g, "\r\n"));
	} else {
		$("#message").val('充电报文不存在');
	}
	$("#Message").modal("show");
}
function messageSure(){
	$("#Message").modal("hide");
}

function goback() {
	$("#detailID", parent.document).val("backFlag");
	window.history.go(-1);
}